package cn.iocoder.an.module.business.controller.admin.equipmentcheckconfig;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepconfig.vo.BindSaveOrDeleteVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentcheckconfig.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentcheckconfig.EquipmentCheckConfigDO;
import cn.iocoder.an.module.business.service.equipmentcheckconfig.EquipmentCheckConfigService;

@Tag(name = "管理后台 - 设备点检配置")
@RestController
@RequestMapping("/business/equipment-check-config")
@Validated
public class EquipmentCheckConfigController {

    @Resource
    private EquipmentCheckConfigService equipmentCheckConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建设备点检配置")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:create')")
    public CommonResult<Long> createEquipmentCheckConfig(@Valid @RequestBody EquipmentCheckConfigSaveReqVO createReqVO) {
        return success(equipmentCheckConfigService.createEquipmentCheckConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备点检配置")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:update')")
    public CommonResult<Boolean> updateEquipmentCheckConfig(@Valid @RequestBody EquipmentCheckConfigSaveReqVO updateReqVO) {
        equipmentCheckConfigService.updateEquipmentCheckConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备点检配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckConfig(@RequestParam("id") Long id) {
        equipmentCheckConfigService.deleteEquipmentCheckConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备点检配置")
                @PreAuthorize("@ss.hasPermission('business:equipment-check-config:delete')")
    public CommonResult<Boolean> deleteEquipmentCheckConfigList(@RequestParam("ids") List<Long> ids) {
        equipmentCheckConfigService.deleteEquipmentCheckConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备点检配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:query')")
    public CommonResult<EquipmentCheckConfigRespVO> getEquipmentCheckConfig(@RequestParam("id") Long id) {
        EquipmentCheckConfigDO equipmentCheckConfig = equipmentCheckConfigService.getEquipmentCheckConfig(id);
        return success(BeanUtils.toBean(equipmentCheckConfig, EquipmentCheckConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备点检配置分页")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:query')")
    public CommonResult<PageResult<EquipmentCheckConfigRespVO>> getEquipmentCheckConfigPage(@Valid EquipmentCheckConfigPageReqVO pageReqVO) {
        PageResult<EquipmentCheckConfigDO> pageResult = equipmentCheckConfigService.getEquipmentCheckConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentCheckConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备点检配置 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-check-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEquipmentCheckConfigExcel(@Valid EquipmentCheckConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentCheckConfigDO> list = equipmentCheckConfigService.getEquipmentCheckConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备点检配置.xls", "数据", EquipmentCheckConfigRespVO.class,
                        BeanUtils.toBean(list, EquipmentCheckConfigRespVO.class));
    }

    @PostMapping("/batchConfig")
    @Operation(summary = "批量配置")
    public CommonResult<Integer>  createEquipmentKeepConfig(@RequestBody BindSaveOrDeleteCheckVO vo) {
        return success(equipmentCheckConfigService.batchEquipmentCheckConfig(vo));
    }

}